package com.u2ds.dss.tools.util;

import com.u2ds.core.util.DateUtil;

import java.util.Date;

/**
 * Created by Ethan.Yuan on 2017/1/19.
 * 设定一个时间段内场地可以按多长的时间段租用
 * 如果开始时间或者结束时间为空，则代表一天的开始(结束)
 */
public class TimeRule {

    private Date beginTime;

    private Date endTime;

    private Integer type;

    /**
     * 计量单位，即可按照多长的时长租用
     */
    private Float unit;

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        Date nextDay = DateUtil.addMinutes(DateUtil.addDay(DateUtil.formateDate(this.beginTime == null ? new Date() : this.beginTime, DateUtil.DATE_FORMAT_yyyy__MM__dd), 1), -1);
        if(endTime.after(nextDay)){
            this.endTime = nextDay;
        }else{
            this.endTime = endTime;
        }
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Float getUnit() {
        return unit;
    }

    public void setUnit(Float unit) {
        this.unit = unit;
    }

    public TimeRule(){
        this.beginTime = DateUtil.addHours(DateUtil.formateDate(new Date(), DateUtil.DATE_FORMAT_yyyy__MM__dd), 8d);
        this.endTime = DateUtil.addHours(this.beginTime, 14d);
    }

    public TimeRule(Date beginTime){
        this.beginTime = beginTime;
        this.setEndTime(DateUtil.addHours(this.beginTime, 14d));
    }

    public TimeRule(Date beginTime, Date endTime){
        this.beginTime = beginTime;
        this.setEndTime(endTime);
    }

    public Date addUnitByMinutes(Date date){
        return DateUtil.addMinutes(date, (int)Float.parseFloat(String.valueOf(unit * 60)));
    }
}
